package net.xxcl.exam.service;

import java.util.*;

import net.xxcl.exam.mapper.ClassMapper;
import net.xxcl.exam.pojo.Criteria;
import net.xxcl.exam.pojo.Tclass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional()
public class ClassService {
    @Autowired
    private ClassMapper classMapper;

    private static final Logger logger = LoggerFactory.getLogger(ClassService.class);

    @Transactional(readOnly = true)
    public int countByExample(Criteria example) {
        int count = this.classMapper.countByExample(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Transactional(readOnly = true)
    public Tclass selectByPrimaryKey(Integer id) {
        return this.classMapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    public List<Tclass> selectByExample(Criteria example) {
        return this.classMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(Integer id) {
        return this.classMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Tclass record) {
        return this.classMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Tclass record) {
        return this.classMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(Criteria example) {
        return this.classMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Tclass record, Criteria example) {
        return this.classMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Tclass record, Criteria example) {
        return this.classMapper.updateByExample(record, example);
    }

    public int insert(Tclass record) {
        return this.classMapper.insert(record);
    }

    public int insertSelective(Tclass record) {
        return this.classMapper.insertSelective(record);
    }

    /**
     * 删除组织
     * @param ids
     * @return
     */
    public int deleteTclass(String ids) {
        String[] str = ids.split(",");
        for(String id:str){
            this.classMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }
        return str.length;
    }

    /**
     * 获取组织树
     * @return
     */
    @Transactional(readOnly = true)
    public List getTclassesTree(){
        Criteria criteria = new Criteria();
        criteria.setOrderByClause("id asc");
        List<Tclass> tclasses = classMapper.selectByExample(criteria);
        Tclass tclass = null;
        //判断哪个是根节点
        for (Tclass t:tclasses){
            if (t.getParentid()==0){
                tclass = t;
                break;
            }
        }
        if(tclasses.size()>0){
            Map<String,Object> map = tclassSimpleTree(tclasses, tclass);
            List list = new ArrayList();
            list.add(map);
            return list;
        }
        return null;
    }

    @Transactional(readOnly = true)
    private Map<String,Object> tclassSimpleTree(List<Tclass> tclasss,Tclass tclass){  //生成菜单树
        Map<String,Object> map = new HashMap<String, Object>();
        if(getChildList(tclasss,tclass).size()>0){
            if(tclass.getId()!=0){    //根节点在前台由extjs指定，不从后台读取
                map.put("id",tclass.getId());
                map.put("qtip",tclass.getDescription());
                map.put("text",tclass.getName());
                map.put("parentId",tclass.getParentid());
                map.put("icon",tclass.getImage());
                map.put("expanded",true);
            }
            List childList = getChildList(tclasss,tclass);
            List list = new ArrayList();
            Iterator it = childList.iterator();
            while(it.hasNext()){
                Tclass m = (Tclass)it.next();
                list.add(tclassSimpleTree(tclasss, m));
            }
            map.put("children",list);
        }else{
            map.put("id",tclass.getId());
            map.put("qtip",tclass.getDescription());
            map.put("text",tclass.getName());
            map.put("parentId",tclass.getParentid());
            map.put("icon",tclass.getImage());
            map.put("leaf",true);
        }
        return map;
    }

    @Transactional(readOnly = true)
    private List getChildList(List<Tclass> list, Tclass tclass){  //得到子节点列表
        List li = new ArrayList();
        Iterator it = list.iterator();
        while(it.hasNext()){
            Tclass m = (Tclass)it.next();
            //注意，integer类型不能用==，基本类型才可以
            if(m.getParentid().equals(tclass.getId())){
                li.add(m);
            }
        }
        return li;
    }
}